package com.spsoft.core.validation;


import com.spsoft.core.utils.EnumUtils;
import com.spsoft.core.utils.StringUtil;
import com.spsoft.core.annotation.EnumClassValue;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.List;

/**
 *  对注解EnumClassValue 验证类
 * @author: cdl
 * @create: 2022/6/30 12:55
 **/
public class EnumClassValueValidator implements ConstraintValidator<EnumClassValue, Object> {


    private Class<? extends Enum<?>> clazz;
    private String fieldValue;

    @Override
    public void initialize(EnumClassValue constraintAnnotation) {

        clazz=constraintAnnotation.clazz();
        fieldValue=constraintAnnotation.fieldValue();
    }
    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {

        List<Object> list = EnumUtils.getFieldValues(clazz,fieldValue);
        if(list.contains(value)|| StringUtil.isEmpty(value)){
            return true;
        }

        return false;

    }
}
